package cn.itheima.controller.user;

import cn.itheima.controller.base.BaseComtroller;
import cn.itheima.domain.Company;
import cn.itheima.domain.Dept;
import cn.itheima.domain.User;
import cn.itheima.service.dept.DeptService;
import cn.itheima.service.user.UserService;
import cn.itheima.util.UtilFuns;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;
@SuppressWarnings("all")
@Controller
@RequestMapping(value = "/system/user")
public class UserController extends BaseComtroller {
    @Autowired
    private UserService userService;
    @Autowired
    private DeptService deptService;

    @RequestMapping(name = "查询用户", value = "/list")
    public String findAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize) {
        PageInfo pb = userService.findAll(pageNum, pageSize,company_id);
        request.setAttribute("pb", pb);

        return "system/user/user-list";
    }
    @RequestMapping(name = "查询用户", value = "/toAdd")
    public String toAdd() {
        List<Dept> list = deptService.toAdd(company_id);
        request.setAttribute("deptList",list);
        return "system/user/user-add";
    }
    @RequestMapping(name = "添加和修改用户", value = "/edit")
    public String edit(User user) {
        user.setCompanyId(company_id);
        user.setCompanyName(company_name);
        if (UtilFuns.isEmpty(user.getId())){
            String s = UUID.randomUUID().toString();
            user.setId(s);
            userService.edit(user);
        }else {
            userService.update(user);

        }
        return "redirect:/system/user/list.do";
    }

    @RequestMapping(name = "返回修改用户的页面", value = "/toUpdate")
    public String toUpdate(String id) {
        List<Dept> list = deptService.toAdd(company_id);
        request.setAttribute("deptList",list);
        User user = userService.findById(id);
        request.setAttribute("user",user);
        return "system/user/user-update";
    }
    @RequestMapping(name = "删除用户", value = "/delete")
    public String delete(String id) {
        System.out.println(id);
        userService.delete(id);

        return "redirect:/system/user/list.do";
    }
}